package com.tianci.appstore;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.coocaa.x.framework.utils.LogUtils;
import com.tianci.appstore.audio.IAudio;
import com.tianci.appstore.controller.AppStoreDetailPageController;
import com.tianci.appstore.ui.data.SkyAppDetailData;
import com.tianci.appstore.ui.data.SkyAppDetailData.AppStatus;
import com.tianci.appstore.ui.detail.AppDetailView;
import com.tianci.appstore.ui.detail.AppDownloadListener;
import com.tianci.appstore.ui.detail.OnDetailRefreshListener;

import java.util.HashMap;
import java.util.Map;

public class AppDetailActivity extends SkyBaseActivity implements OnDetailRefreshListener,
        AppDownloadListener {
    private AppDetailView contentView;

    private final int UPDATE_DATA = 0;
    private final int UPDATE_STATUS = 1;
    private final int UPDATE_PERCENT = 2;

    private String appId;

    private Handler mainHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            switch (msg.what) {
                case UPDATE_DATA:
                    refreshDetailData((SkyAppDetailData) msg.obj);
                    break;
                case UPDATE_STATUS:
                    refreshAppStatus((AppStatus) msg.obj);
                    break;
                case UPDATE_PERCENT:
                    refreshDownloadPercent(msg.arg1);
                    break;
            }
            super.handleMessage(msg);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        contentView = new AppDetailView(this);
        setContentView(contentView);
        contentView.clearContent();
        Intent intent = this.getIntent();
        appId = intent.getStringExtra("id");
        AppStoreDetailPageController.getInstance().init(appId, this, this);
        initAudio();
        LogUtils.d("mem", "oncrate detalils");
    }

    @Override
    protected void onNewIntent(Intent intent) {
        // TODO Auto-generated method stub
        contentView.clearContent();
        appId = intent.getStringExtra("id");
        AppStoreDetailPageController.getInstance().init(appId, this, this);
        super.onNewIntent(intent);
    }

    @Override
    protected Map<String, String> getPageStartParams() {
        HashMap<String,String> map = new HashMap<String,String>();
        map.put("app_id",appId);
        return map;
    }

    public void refreshDetailData(SkyAppDetailData data) {
        contentView.update(data);
    }

    public void refreshAppStatus(AppStatus status) {
        contentView.updateStatus(status);
    }

    public void refreshDownloadPercent(int percent) {
        contentView.updateDownloadPercent(percent);
    }

    // private SkyAppDetailData createTestData(AppStatus status){
    // String detailStr = "故事围绕一位人类大兵的生活展开。讲述了沉睡的古代巨型怪兽被人们意外唤醒，醒来后的怪兽展现出强大的破坏能力，"
    // + "它的存在也震惊了世界，庞大巨兽的降临将给整个世界带来毁天灭地的灾难。" + "这是一个关于人类的勇气与和谐的故事。当强大的哥斯拉出现，人类直面自然界的巨大力量"
    // + "而完全无法抵抗时，人们将如何试图重建和平？ " + "mu夫妻被ntr了，美帝被爆菊了，大兵被凌辱了，"
    // + "中国闹革命了，韩日本被轰沉了，教育局不再了，光腚局成历史了，傻逼逗比都从世界上烟消云散了，"
    // + "哥斯拉终于成为老大哥了——p.s电影都发展到这地步了，小时代还能有票房的话我就无语了";
    // SkyAppDetailData data = new SkyAppDetailData(
    // "id",
    // "测试新应用名称",
    // "http://a.hiphotos.baidu.com/image/w%3D400/sign=dabb9b9ea60f4bfb8cd09f54334e788f/060828381f30e924e02068304e086e061d95f72f.jpg",
    // status, detailStr);
    // List<String> shotList = new ArrayList<String>();
    // shotList.add("http://b.hiphotos.baidu.com/image/w%3D400/sign=a0cf38e05b82b2b7a79f38c401accb0a/f11f3a292df5e0fe98ae29e55e6034a85fdf729b.jpg");
    // shotList.add("http://f.hiphotos.baidu.com/image/w%3D400/sign=ec57596b6e81800a6ee5880e813433d6/a5c27d1ed21b0ef426da38badfc451da81cb3e27.jpg");
    // shotList.add("http://b.hiphotos.baidu.com/image/w%3D400/sign=80819f66544e9258a63487eeac83d1d1/c2cec3fdfc0392455f2a960d8594a4c27d1e2510.jpg");
    // shotList.add("http://h.hiphotos.baidu.com/image/w%3D400/sign=5cb01c05fadcd100cd9cf921428a47be/d833c895d143ad4bb664f0c880025aafa40f066b.jpg");
    // data.setScreenShotsPreview(shotList);
    // List<String> infoList = new ArrayList<String>();
    // infoList.add("version:3.3.3.3");
    // infoList.add("大小：13.25M");
    // infoList.add("类型：网络工具类");
    // infoList.add("下载：XXXXX次");
    // infoList.add("操作：遥控器");
    // data.setInfoList(infoList);
    // return data;
    // }

    @Override
    public void onUpdate(SkyAppDetailData appDetail) {
        // TODO Auto-generated method stub
        LogUtils.d("detail", "detail data came back !!!!!!!!!!!!!!!!!");
        Message msg = mainHandler.obtainMessage(UPDATE_DATA);
        msg.obj = appDetail;
        mainHandler.sendMessage(msg);
    }

    @Override
    public void onUpdateStatus(String appId, AppStatus status) {
        // TODO Auto-generated method stub
        if (!appId.equals(this.appId))
            return;
        Message msg = mainHandler.obtainMessage(UPDATE_STATUS);
        msg.obj = status;
        mainHandler.sendMessage(msg);
    }

    @Override
    public void onUpdatePercent(String appId, int percent) {
        // TODO Auto-generated method stub
        if (!appId.equals(this.appId))
            return;
        Message msg = mainHandler.obtainMessage(UPDATE_PERCENT);
        msg.arg1 = percent;
        mainHandler.sendMessage(msg);
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        contentView.destory();
        AppStoreDetailPageController.getInstance().destroy();
        super.onDestroy();
    }

    @Override
    public String getName() {
        // TODO Auto-generated method stub
        return "应用圈应用详情页面";
    }

    @Override
    public IAudio getAudioInterface() {
        return AppStoreDetailPageController.getInstance();
    }

}
